<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="20%"
    :before-close="handleClose"
  >
    <div>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="参与人" prop="uuid">
          <el-tree-select
            default-expand-all
            node-key="id"
            filterable
            v-model="ruleForm.uuid"
            :data="treeData"
            check-strictly
            :render-after-expand="false"
            :props="{
              label: 'name',
            }"
            style="width: 100%"
            placeholder="选择参与人"
            clearable
          >
          </el-tree-select>
        </el-form-item>
        <el-form-item label="角色" prop="postion">
          <el-select
            style="width: 100%"
            v-model="ruleForm.postion"
            placeholder="角色"
          >
            <el-option
              v-for="item in roleOptions"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { treePersonApi as listApi } from "@/plugins/api/system/user";
import { userAddApi, userSaveApi } from "@/plugins/api/customer/manage/user";
// 字典查询
import { listApi as dictApi } from "@/plugins/api/system/dict";
const props = defineProps({
  dataInfo: {
    type: Object,
    default: {},
  },
});
// 获取字典项
let roleOptions = ref([]);
function getDictionary(dict_id) {
  return new Promise((resolve, reject) => {
    dictApi({ dict_id: dict_id }).then((data) => {
      resolve(data);
    });
  });
}
let treeData = ref([]);
function getUsers() {
  listApi().then((res) => {
    treeData.value = res;
  });
}
let ruleForm = ref({});
let rules = {
  uuid: [{ required: true, message: "选择参与人", trigger: "change" }],
  postion: [{ required: true, message: "选择角色", trigger: "change" }],
};
let dialogVisible = ref(false);
function handleClose(done) {
  // dialogVisible.value = false;
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
}
let title = ref("");
let ruleFormRef = ref(null);
function initFun(type, row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  ruleForm.value = {};
  if (type == "add") {
    title.value = "新增";
  } else {
    ruleForm.value = { ...row };
  }

  dialogVisible.value = true;
  getDictionary(17).then((data) => {
    roleOptions.value = data;
  });
  getUsers();
}
const emit = defineEmits(["refreshUser"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      let obj = { ...ruleForm.value };
      obj.opportunity_id = props.dataInfo.id;
      if (!ruleForm.value.id) {
        userAddApi(obj).then((data) => {
          ElMessage.success("新增成功！");
          dialogVisible.value = false;
          emit("refreshUser");
        });
      } else {
        userSaveApi(obj).then((data) => {
          ElMessage.success("修改成功！");
          dialogVisible.value = false;
          emit("refreshUser");
        });
      }
    }
  });
}

defineExpose({
  initFun,
});
</script>
<style scoped lang="scss"></style>
